(function() {
  var DataSources, MyPageView, Wine, WineDetailsView, Wines, WinesListView, WinesPageView, template,
    __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };

  template = require('views/templates/wines_page');

  MyPageView = require('views/base/custom/my_page_view');

  Wines = require('models/wines');

  Wine = require('models/wine');

  WinesListView = require('./wines_list_view');

  WineDetailsView = require('./wine_details_view');

  DataSources = require('dataaccess/data_sources');

  module.exports = WinesPageView = (function(_super) {

    __extends(WinesPageView, _super);

    WinesPageView.name = 'WinesPageView';

    function WinesPageView() {
      this.renderWineDetails = __bind(this.renderWineDetails, this);

      this.renderWinesList = __bind(this.renderWinesList, this);

      this.onCollectionChanged = __bind(this.onCollectionChanged, this);

      this.subscribeToControlsEvents = __bind(this.subscribeToControlsEvents, this);

      this.subscribeToDataSourceEvents = __bind(this.subscribeToDataSourceEvents, this);
      return WinesPageView.__super__.constructor.apply(this, arguments);
    }

    WinesPageView.prototype.container = '#page-container';

    WinesPageView.prototype.template = template;

    WinesPageView.prototype.autoRender = false;

    WinesPageView.prototype.id = "wines-page";

    WinesPageView.prototype.listContainerName = '.wine-list-container';

    WinesPageView.prototype.itemContainerName = '.wine-item-details';

    WinesPageView.prototype.wineDetailsViewName = 'wineDetails';

    WinesPageView.prototype.wineListViewName = 'winesList';

    WinesPageView.prototype.winesList = null;

    WinesPageView.prototype.initialize = function() {
      console.debug('WinesPageView#initialize-Start');
      this.collection = DataSources.GetWines();
      console.debug('WinesPageView#intialize-End');
      return WinesPageView.__super__.initialize.apply(this, arguments);
    };

    WinesPageView.prototype.subscribeToDataSourceEvents = function() {
      this.modelBind('add', this.closeDetails);
      this.modelBind('destroy', this.closeDetails);
      return this.modelBind('change', this.onCollectionChanged);
    };

    WinesPageView.prototype.subscribeToControlsEvents = function() {
      this.delegate('click', '.createNewWine', this.createNewWine);
      this.unsubscribeEvent(WinesListView.selectedItemEventName, this.renderWineDetails);
      return this.subscribeEvent(WinesListView.selectedItemEventName, this.renderWineDetails);
    };

    WinesPageView.prototype.render = function() {
      console.debug('WinesPageView#render', $(this.container), this.$el);
      $(this.container).html(this.$el);
      return WinesPageView.__super__.render.apply(this, arguments);
    };

    WinesPageView.prototype.renderSubviews = function() {
      WinesPageView.__super__.renderSubviews.apply(this, arguments);
      console.debug('WinesPageView#renderSubviews-Start');
      return this.renderWinesList();
    };

    WinesPageView.prototype.closeDetails = function() {
      return this.closeSubView(this.wineDetailsViewName);
    };

    WinesPageView.prototype.onCollectionChanged = function() {
      this.subscribeToControlsEvents();
      return this.renderWinesList();
    };

    WinesPageView.prototype.renderWinesList = function() {
      console.debug('WinesPageView#renderWinesList-Start');
      this.closeDetails();
      this.closeSubView(this.wineListViewName);
      this.winesList = new WinesListView({
        collection: this.collection
      });
      this.winesList.containerSelector = this.listContainerName;
      this.subview(this.wineListViewName, this.winesList);
      return this.winesList.render();
    };

    WinesPageView.prototype.createNewWine = function() {
      console.debug('WinesPageView#createNewWine-Start');
      this.renderWineDetails(new Wine());
      return console.debug('WinesPageView#createNewWine-End');
    };

    WinesPageView.prototype.renderWineDetails = function(wine) {
      var view;
      console.debug('WinesPageView#renderWineDetails-Start' + wine.id);
      view = new WineDetailsView({
        model: wine,
        container: this.$(this.itemContainerName)
      });
      this.subview(this.wineDetailsViewName, view);
      view.render();
      return console.debug('WinesPageView#renderWineDetails-End' + wine.id);
    };

    return WinesPageView;

  })(MyPageView);

}).call(this);
